﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Net.Http;
using System.Net.Http.Headers;
using TPLOAWebClient.Resources;
using System.Web.Security;

namespace TPLOAWebClient
{
    public partial class staffprofile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            currentTbx.Attributes["value"] = currentTbx.Text;
            newTbx.Attributes["value"] = newTbx.Text;
            confirmTbx.Attributes["value"] = confirmTbx.Text;
            HttpClient client = (HttpClient)Session["httpclient"];
            HttpResponseMessage response = null;

            if (Session["username"] != null)
            {
                response = client.GetAsync("api/members/" + Session["username"]).Result;
                var member = response.Content.ReadAsAsync<Member>().Result;
                if (member.UserType.Equals("Student"))
                {
                    Response.Redirect("studentprofile.aspx");
                }
                else if (member.UserType.Equals("Clinic"))
                {
                    Response.Redirect("clinicprofile.aspx");
                }
                else if (member.UserType.Equals("Admin"))
                {
                    Response.Redirect("adminprofile.aspx");
                }

                response = client.GetAsync("api/Staffs/" + Session["username"]).Result;
                var staff = response.Content.ReadAsAsync<Staff>().Result;
                if (!staff.ApprovePermissions)
                {
                    viewloaBtn.Visible = false;
                }
                else
                {
                    viewloaBtn.Visible = true;
                }
            }
            if (!Page.IsPostBack)
            {
                response = client.GetAsync("api/Staffs/" + Session["username"]).Result;  // Blocking call!
                if (response.IsSuccessStatusCode)
                {
                    // Parse the response body. Blocking!
                    var staff = response.Content.ReadAsAsync<Staff>().Result;
                    staffidLbl.Text = staff.StaffID;
                    nameTbx.Text = staff.Name;
                    emailTbx.Text = staff.Email;
                    coursecodeLbl.Text = staff.CourseCode;
                    if (staff.AdminRights == true)
                    {
                        adminrightsLbl.Text = "Yes";
                    }
                    else
                    {
                        adminrightsLbl.Text = "No";
                    }
                    if (staff.ApprovePermissions == true)
                    {
                        approvepermissionLbl.Text = "Yes";
                    }
                    else
                    {
                        approvepermissionLbl.Text = "No";
                    }
                }
                else
                {
                    retrieveLbl.Text += response.StatusCode + "(" + response.ReasonPhrase + ")";
                }
            }
        }

        protected void updateBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                HttpClient client = (HttpClient)Session["httpclient"];

                bool approvepermission = false;
                if (approvepermissionLbl.Text == "Yes")
                {
                    approvepermission = true;
                }
                bool adminright = false;
                if (adminrightsLbl.Text == "Yes")
                {
                    adminright = true;
                }
                var staff = new Staff()
                {
                    StaffID = staffidLbl.Text,
                    Name = nameTbx.Text,
                    Email = emailTbx.Text,
                    CourseCode = coursecodeLbl.Text,
                    ApprovePermissions = approvepermission,
                    AdminRights = adminright
                };
                HttpResponseMessage response = client.PutAsJsonAsync("api/Staffs/" + staffidLbl.Text, staff).Result;  // Blocking call!
                if (response.IsSuccessStatusCode)
                {
                    retrieveLbl.Visible = true;
                    retrieveLbl.Text = "Profile Updated at " +DateTime.Now.ToLongDateString()+" "+DateTime.Now.ToLongTimeString();
                }
                else
                {
                    retrieveLbl.Visible = true;
                    retrieveLbl.Text = "Error";
                }
            }
        }

        protected void profileBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("staffprofile.aspx");
        }

        protected void viewloaBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("staffviewloa.aspx");
        }

        protected void submitloaBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("staffsubmitloa.aspx");
        }

        protected void faqBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("stafffaq.aspx");
        }

        protected void otherservicesBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("staffotherservice.aspx");
        }

        protected void LogoutBtn_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Redirect("default.aspx");
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked)
            {
                currentTbx.TextMode = TextBoxMode.SingleLine;
            }
            else
            {
                currentTbx.TextMode = TextBoxMode.Password;

            }
        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            string text = newTbx.Text;
            if (CheckBox2.Checked)
            {
                newTbx.TextMode = TextBoxMode.SingleLine;
            }
            else
            {
                newTbx.TextMode = TextBoxMode.Password;
            }
        }

        protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox3.Checked)
            {
                confirmTbx.TextMode = TextBoxMode.SingleLine;
            }
            else
            {
                confirmTbx.TextMode = TextBoxMode.Password;
            }
        }

        protected void changeBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                HttpClient client = (HttpClient)Session["httpclient"];
                HttpResponseMessage response = client.GetAsync("api/members/" + staffidLbl.Text).Result;
                if (response.IsSuccessStatusCode)
                {
                    var checkpassword = response.Content.ReadAsAsync<Member>().Result;
                    if (checkpassword.Password == currentTbx.Text)
                    {
                        var member = new Member()
                        {
                            Username = staffidLbl.Text,
                            UserType = "Admin",
                            Password = newTbx.Text
                        };
                        HttpResponseMessage response2 = client.PutAsJsonAsync("api/members/" + staffidLbl.Text, member).Result;  // Blocking call!
                        if (response2.IsSuccessStatusCode)
                        {
                            changeLbl.Text = "";
                            changeLbl.Visible = true;
                            changeLbl.Text = "Password Updated at " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
                        }
                        else
                        {
                            changeLbl.Text = "";
                            changeLbl.Visible = true;
                            changeLbl.Text = response.StatusCode + "(" + response.ReasonPhrase + ")";
                        }
                    }
                    else
                    {
                        RequiredFieldValidator3.IsValid = false;
                        RequiredFieldValidator3.ErrorMessage = "Your current password is wrong";
                    }
                }
            }
        }
    }
}